package com.quectel.core.module.alarm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.alarm.dao.AlarmEventPushUserDao;
import com.quectel.core.module.alarm.dto.AlarmEventPushUserDto;
import com.quectel.core.module.alarm.entity.AlarmEventPushUserEntity;
import com.quectel.core.module.alarm.service.AlarmEventPushUserService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-27 16:03:51
 */
@DubboService
public class AlarmEventPushUserServiceImpl implements AlarmEventPushUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AlarmEventPushUserServiceImpl.class);

    private static final String ALARM_EVENT_PUSH_USER_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "ALARM_EVENT_PUSH_USER_CACHE_BY_ID:";
    private static final String ALARM_EVENT_PUSH_USER_CACHE_BY_EVENT_CONFIG_ID = RedisCacheConstants.PROJECT_NAME + "ALARM_EVENT_PUSH_USER_CACHE_BY_EVENT_CONFIG_ID:";

    @Autowired
    private AlarmEventPushUserDao alarmEventPushUserDao;


    @Override
    public List<AlarmEventPushUserDto> selectCacheByAlarmEventConfigId(Long alarmEventConfigId) {
        return CacheKit.cacheToRedis(
                () -> selectByAlarmEventConfigId(alarmEventConfigId),
                ALARM_EVENT_PUSH_USER_CACHE_BY_EVENT_CONFIG_ID + alarmEventConfigId,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public List<AlarmEventPushUserDto> selectByAlarmEventConfigId(Long alarmEventConfigId) {
        List<AlarmEventPushUserEntity> entityList = alarmEventPushUserDao.selectList(new LambdaQueryWrapper<AlarmEventPushUserEntity>()
                .eq(AlarmEventPushUserEntity::getAlarmEventConfigId, alarmEventConfigId));
        return CopyUtils.copyList(entityList, AlarmEventPushUserDto.class);
    }

    @Override
    public AlarmEventPushUserDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                ALARM_EVENT_PUSH_USER_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public AlarmEventPushUserDto selectById(Long id) {
        AlarmEventPushUserEntity entity = alarmEventPushUserDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, AlarmEventPushUserDto.class));
    }

    @Override
    public List<AlarmEventPushUserDto> queryList(Map<String, Object> params) {
        List<AlarmEventPushUserEntity> list = alarmEventPushUserDao.queryList(params);
        List<AlarmEventPushUserDto> result = CopyUtils.copyList(list, AlarmEventPushUserDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private AlarmEventPushUserDto paddingField(AlarmEventPushUserDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return alarmEventPushUserDao.queryTotal(params);
    }

    @Override
    public Long save(AlarmEventPushUserDto dto) {
        AlarmEventPushUserEntity entity = CopyUtils.copyObj(dto, AlarmEventPushUserEntity.class);
        entity.setId(Snowflake.nextId());
        alarmEventPushUserDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AlarmEventPushUserDto dto) {
        AlarmEventPushUserEntity entity = CopyUtils.copyObj(dto, AlarmEventPushUserEntity.class);
        alarmEventPushUserDao.updateById(entity);
        CacheKit.invalidRedisCache(ALARM_EVENT_PUSH_USER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(AlarmEventPushUserDto dto) {
        AlarmEventPushUserEntity entity = CopyUtils.copyObj(dto, AlarmEventPushUserEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, alarmEventPushUserDao);

        CacheKit.invalidRedisCache(ALARM_EVENT_PUSH_USER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        alarmEventPushUserDao.deleteById(id);

        CacheKit.invalidRedisCache(ALARM_EVENT_PUSH_USER_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
